﻿using Microsoft.AspNetCore.Mvc;
using NoIdeaIoc;
using NoIdeaWebApi.Dtos;

namespace NoIdeaWebApi.Apis;

public class HomeController : ControllerBase
{
    readonly TestIoc testIoc;
    readonly ILogger logger;
    readonly ILifeTime lifeTime;
    public HomeController(TestIoc testIoc, ILogger logger, ILifeTime lifeTime)
    {
        this.testIoc = testIoc;
        this.logger = logger;
        this.lifeTime = lifeTime;
    }
    [HttpGet]
    public IActionResult TestLog()
    {
        logger.LogWarning($"测试警告日志......{DateTime.Now}");
        return Ok(new NoIdeaIoc.JsonResponse { Data = "响应数据", Msg = "测试成功" });
    }
    [HttpGet]
    public IActionResult Test()
    {
        return Ok(new NoIdeaIoc.JsonResponse { Data = "响应数据Json", Msg = "测试成功" });
    }
    [HttpGet]
    public IActionResult Index()
    {
        return base.Content("测试响应结果Html", "text/html;charset=utf-8");
    }
    [HttpGet]
    public IActionResult TestIoc()
    {
        return Ok(testIoc);
    }
    [HttpGet]
    public async Task<IActionResult> TestTransient()
    {
        var dto1 = lifeTime.Resolve<TestTransientDto>();
        var dto2 = lifeTime.Resolve<TestTransientDto>();
        var dto3 = lifeTime.Resolve<TestTransientDto>();
        var dto4 = lifeTime.Resolve<TestTransientDto>();
        return Ok(new NoIdeaIoc.JsonResponse { Data = new { Dto1 = dto1, Dto2 = dto2, Dto3 = dto3, Dto4 = dto4 }, Msg = "测试 Transient 成功" });
    }
    [HttpGet]
    public async Task<IActionResult> TestScoped()
    {
        var dto1 = lifeTime.Resolve<TestScopedDto>();
        var dto2 = lifeTime.Resolve<TestScopedDto>();
        var dto3 = lifeTime.Resolve<TestScopedDto>();
        var dto4 = lifeTime.Resolve<TestScopedDto>();
        return Ok(new NoIdeaIoc.JsonResponse { Data = new { Dto1 = dto1, Dto2 = dto2, Dto3 = dto3, Dto4 = dto4 }, Msg = "测试 Scoped 成功" });
    }
    [HttpGet]
    public async Task<IActionResult> TestSingleton()
    {
        var dto1 = lifeTime.Resolve<TestSingletonDto>();
        var dto2 = lifeTime.Resolve<TestSingletonDto>();
        var dto3 = lifeTime.Resolve<TestSingletonDto>();
        var dto4 = lifeTime.Resolve<TestSingletonDto>();
        return Ok(new NoIdeaIoc.JsonResponse { Data = new { Dto1 = dto1, Dto2 = dto2, Dto3 = dto3, Dto4 = dto4 }, Msg = "测试 Singleton 成功" });
    }
}
